﻿#include "PCIControl.h"

PCIControl::PCIControl(QObject *parent)
	: QObject(parent)
{
	init();
	//connect(this, &PCIControl::getDataOnceOver, PtrCoreData, &CoreData::handleDatas,Qt::QueuedConnection);
	qDebug() << "PCIControl Init Finished...";
}

PCIControl::~PCIControl()
{
	m_ctrl->Release();
}

void PCIControl::init()
{
	m_ctrl = WaveformAiCtrl::Create();
	Array<DeviceTreeNode> *supportedDevice = m_ctrl->getSupportedDevices();
	DeviceInformation devInfo;
	if (supportedDevice->getCount() == 0)
	{
		qDebug() << "无可用设备......";
	}
	else
	{
		qDebug() << "可用设备为：" << supportedDevice->getCount();
		for (int i = 0; i < 1; i++)
		{
			DeviceTreeNode const &node = supportedDevice->getItem(i);
			qDebug("%d, %ls\n", node.DeviceNumber, node.Description);
			qDebug() << node.DeviceNumber;
			qDebug() << node.Description;
			devInfo.DeviceNumber = node.DeviceNumber;
			m_ctrl->setSelectedDevice(devInfo);
			//if (m_ctrl->getFeatures()->getRetriggerable())
			//{
			//	ui.cmbDevice->addItem(QString::fromWCharArray(node.Description));
			//}
			if (node.DeviceNumber == 1)
			{
				m_ctrl->setSelectedDevice(devInfo);
			}

		}
	}
}

bool PCIControl::loadProfile(QString xmlFilePath)
{
	//wchar_t *wcharConfigFilePath;
	std::wstring profile = xmlFilePath.toStdWString();
	//int a = xmlFilePath.toWCharArray(wcharConfigFilePath);
	ret = m_ctrl->LoadProfile(profile.c_str());
	if (ret == Success)
	{
		qDebug() << "Load Profile Success...";

	}
	else
	{
		qDebug() << "Error Code:" << ret;
		QString message = tr("Sorry, there are some errors occurred, Error Code: 0x") +
			QString::number(ret, 16).right(8).toUpper();
		qDebug() << "Error Code Message:" << message;
		qDebug() << "Load Profile Error!!!" << profile;
		qDebug() << "Load Profile Error!!!" << profile.c_str();
		return false;

	}
	m_ctrl->addDataReadyHandler(OnDataReadyEvent, this);
	//m_ctrl->addCacheOverflowHandler(OnCacheOverflowEvent, this);
	//m_ctrl->addOverrunHandler(OnOverRunEvent, this);
	//m_ctrl->addStoppedHandler(OnStoppedEvent, this);
	return true;
}

bool PCIControl::prepare()
{
	ret = m_ctrl->Prepare();
	if (ret == Success)
	{
		qDebug() << "PCI Prepare Success......";
		return true;
	}
	else {
		qDebug() << "PCI Prepare Error!!!";
		return false;
	}
}

bool PCIControl::start()
{
	ret = m_ctrl->Start();
	if (ret == Success)
	{
		qDebug() << "PCI Start Success......";
		return true;
	}
	else {
		qDebug() << "PCI Start Error!!!";
		return false;
	}
}

bool PCIControl::stop()
{
	ret = m_ctrl->Stop();
	if (ret == Success)
	{
		qDebug() << "PCI Stop Success......";
		return true;
	}
	else {
		qDebug() << "PCI Stop Error!!!";
		return false;
	}
}

//double * PCIControl::getDatas()
//{
//	return m_datas;
//}

void BDAQCALL PCIControl::OnDataReadyEvent(void * sender, BfdAiEventArgs * args, void * userParam)
{
	qDebug()<< "OnDataReadyEvent";
	PCIControl * uParam = (PCIControl *)userParam;
	int32 remainingCount = args->Count;
	qDebug() << "args->count:" << remainingCount;
	//配置文件中设定长度
	int32 bufSize = ONCE_DATA_LENGTH;
	
	uParam->m_index = uParam->m_count % SAVE_DATA_LENGTH;
	qDebug()<<"index:" << uParam->m_index<<"m_count:"<< uParam->m_count;
	//double *dataZone = uParam->m_dataPtrVec[index];
	double *dataZone = PtrCoreData->getDataPtrWithIndex(uParam->m_index);
	ErrorCode ret = ((WaveformAiCtrl*)sender)->GetData(bufSize, dataZone);
	if (ret != Success && ret != WarningRecordEnd)
	{
		QString message = tr("Sorry, there are some errors occurred, Error Code: 0x") +
			QString::number(ret, 16).right(8);
		//QMessageBox::information(uParam, "Warning Information", message);
		return;
	}
	if (ret == Success || ret == WarningRecordEnd)
	{
		//qDebug() << "getDataOnceOver" << uParam->m_index;
		emit uParam->getDataOnceOver(uParam->m_index);
		qDebug() << QString("采集到第%1组数据：").arg(++uParam->m_count);
	}
	return;
}

void PCIControl::checkError(const ErrorCode &ret)
{
	switch (ret)
	{
	case Success:
		qDebug() << "Success...";
	case ErrorFuncNotInited:
		qDebug() << "ErrorFuncNotInited...";
		//default:
		//	break;
	}
}

int PCIControl::getIndex()
{
	return m_index;
}
